using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Index.Config
{
    public class IndexConfiguration
    {
        private string coding_method;
        private bool document_gaps;
        private IndexType type;

        public IndexConfiguration(string coding_method, bool document_gaps, IndexType type)
        {
            this.coding_method = coding_method;
            this.document_gaps = document_gaps;
            this.type = type;
        }

        public string CodingMethod
        {
            get
            {
                return coding_method;
            }
        }

        public bool DocumentGaps
        {
            get
            {
                return document_gaps;
            }
        }

        public IndexType IndexType
        {
            get
            {
                return type;
            }
        }

        public static IndexConfiguration DeSerialize(string file_path)
        {
            Stream stream = new FileStream(file_path, FileMode.Open);

            TextReader tr = new StreamReader(stream, Encoding.UTF8);
            string coding_method = tr.ReadLine();
            bool document_gaps = Boolean.Parse(tr.ReadLine());
            string type = tr.ReadLine();
            tr.Close();
            stream.Close();


            if (type.Equals("Positional"))
            {
                return new IndexConfiguration(coding_method, document_gaps, IndexType.Positional);
            }
            else
            {
                return new IndexConfiguration(coding_method, document_gaps, IndexType.Frequencial);

            }
        }

        public void Serialize(string file_path)
        {
            Stream stream = new FileStream(file_path, FileMode.CreateNew);
            TextWriter tw = new StreamWriter(stream, Encoding.UTF8);

            tw.WriteLine(coding_method);
            tw.WriteLine(document_gaps);
            tw.WriteLine(type);
           

            tw.Close();
            stream.Close();
        }
    }
}
